﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Reflection;

namespace Ccit.Utils
{
    /// <summary>
    /// 工厂类
    /// </summary>
    public class Factory
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public Factory() {}

        /// <summary>
        /// 实例化工厂对象
        /// </summary>
        /// <param name="componentName"></param>
        /// <param name="className"></param>
        /// <param name="componentType"></param>
        /// <param name="constructorParams"></param>
        /// <returns></returns>
        public static object CreateObject(string componentName,
                                          string className,
                                          ImportComponentType componentType,
                                          object[] constructorParams)
        {
            const string sBinFolder = "bin";
            string sRelativelyPath = RequestContext.GetRelativelyPath();  //获取相对路径
            string sAppPath = HttpContext.Current.Server.MapPath(sRelativelyPath + sBinFolder) + "\\";
            string sComponentName = sAppPath + componentName;
            string strDbType = getDbTypeName();
            string sClassName = className + strDbType;
            ImportComponentType eComponentType = componentType;

            object loadedObject = null;
            Assembly assembly;
            try
            {
                assembly = Assembly.LoadFrom(sComponentName);
            }
            catch
            {
                return null;
            }

            string sInstanceName = (eComponentType == ImportComponentType.ComPlusDll) ?
                sClassName + "Class" : sClassName;

            int iCount = assembly.GetTypes().Length;
            for (int i = 0; i < iCount; i++)
            {
                System.Type type = assembly.GetTypes()[i];
                string sInstanceType = sInstanceName.Substring(sInstanceName.LastIndexOf(".") + 1);

                if (type.Name == sInstanceType)
                {
                    BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
                    loadedObject = assembly.CreateInstance(sInstanceName, true, BindingFlags.Default, null, constructorParams, null, null);
                   break;
                }
            }

            return loadedObject;
        }

        

        private static string getDbTypeName()
        {
            string sDbType = HttpContext.Current.Application["DBTYPE"].ToString();
            string sTypeName = "";
            switch (sDbType)
            {
                case "1":
                    sTypeName = "SQL";
                    break;
                case "2":
                    sTypeName = "ORA";
                    break;
                case "3":
                    sTypeName = "SYB";
                    break;
                case "4": //access
                    sTypeName = "AES";
                    break;

            }

            return sTypeName;
        }
    }

    
}
